package org.dromara.device.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.device.domain.bo.DeviceStoreAreaBo;
import org.dromara.device.domain.vo.DeviceStoreAreaVo;
import org.dromara.device.domain.DeviceStoreArea;
import org.dromara.device.mapper.DeviceStoreAreaMapper;
import org.dromara.device.service.IDeviceStoreAreaService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 设备管理-设备与门店分区关联Service业务层处理
 *
 * @author samuel
 * @date 2024-03-21
 */
@RequiredArgsConstructor
@Service
public class DeviceStoreAreaServiceImpl implements IDeviceStoreAreaService {

    private final DeviceStoreAreaMapper baseMapper;

    /**
     * 查询设备管理-设备与门店分区关联
     */
    @Override
    public DeviceStoreAreaVo queryById(Long deviceId){
        return baseMapper.selectVoById(deviceId);
    }

    /**
     * 查询设备管理-设备与门店分区关联列表
     */
    @Override
    public TableDataInfo<DeviceStoreAreaVo> queryPageList(DeviceStoreAreaBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DeviceStoreArea> lqw = buildQueryWrapper(bo);
        Page<DeviceStoreAreaVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询设备管理-设备与门店分区关联列表
     */
    @Override
    public List<DeviceStoreAreaVo> queryList(DeviceStoreAreaBo bo) {
        LambdaQueryWrapper<DeviceStoreArea> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DeviceStoreArea> buildQueryWrapper(DeviceStoreAreaBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DeviceStoreArea> lqw = Wrappers.lambdaQuery();
        return lqw;
    }

    /**
     * 新增设备管理-设备与门店分区关联
     */
    @Override
    public Boolean insertByBo(DeviceStoreAreaBo bo) {
        DeviceStoreArea add = MapstructUtils.convert(bo, DeviceStoreArea.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDeviceId(add.getDeviceId());
        }
        return flag;
    }

    /**
     * 修改设备管理-设备与门店分区关联
     */
    @Override
    public Boolean updateByBo(DeviceStoreAreaBo bo) {
        DeviceStoreArea update = MapstructUtils.convert(bo, DeviceStoreArea.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DeviceStoreArea entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除设备管理-设备与门店分区关联
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
